!include LogicLib.nsh
!include "WordFunc.nsh"
!include "FileFunc.nsh"

Function checkYfscreenshot
  CheckProc:
    nsProcessW::_FindProcess "yfscreenshot.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "安装程序检测到[yfscreenshot.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续安装。$\r$\n点击 “取消” 退出安装程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "yfscreenshot.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\yfscreenshot"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function checkYfiletransfer
  CheckProc:
    nsProcessW::_FindProcess "yfx_p2p.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "安装程序检测到[yfx_p2p.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续安装。$\r$\n点击 “取消” 退出安装程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "yfx_p2p.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\yfiletransfer"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function checkMeeting
  CheckProc:
    nsProcessW::_FindProcess "翼丰信会议系统.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "安装程序检测到[翼丰信会议系统.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续安装。$\r$\n点击 “取消” 退出安装程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "翼丰信会议系统.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\Meeting"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function un.checkYfscreenshot
  CheckProc:
    nsProcessW::_FindProcess "yfscreenshot.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "卸载程序检测到[yfscreenshot.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续卸载。$\r$\n点击 “取消” 退出卸载程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "yfscreenshot.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\yfscreenshot"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function un.checkYfiletransfer
  CheckProc:
    nsProcessW::_FindProcess "yfx_p2p.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "卸载程序检测到[yfx_p2p.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续卸载。$\r$\n点击 “取消” 退出卸载程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "yfx_p2p.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\yfiletransfer"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function un.checkMeeting
  CheckProc:
    nsProcessW::_FindProcess "翼丰信会议系统.exe"
    Pop $R0
    ${If} $R0 = 0
      Goto CloseExe
	  ${Else}
      Goto Done
	  ${EndIf}

  CloseExe:
    MessageBox MB_OKCANCEL|MB_ICONSTOP "卸载程序检测到[翼丰信会议系统.exe]正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭，继续卸载。$\r$\n点击 “取消” 退出卸载程序。" IDCANCEL Exit
    nsProcessW::_KillProcess "翼丰信会议系统.exe"
    Sleep 2000
    RMDir /r "$INSTDIR\resources\Meeting"
    Goto CheckProc
  Exit:
    Abort
  Done:
FunctionEnd

Function un.delAppData
  # 对所有用户需要打开all设置（$APPDATA指向C:\ProgramData），对当前用户时为current（$APPDATA指向C:\Users\xxx\AppData\Roaming）
  SetShellVarContext current
  RMDir /r "$APPDATA\yf-pc-chat-yfx"
  #RMDir /r "$LOCALAPPDATA\yf-pc-chat-yfx"
  SetShellVarContext all
FunctionEnd

Function un.delDocument
  # 对所有用户需要打开all设置（$APPDATA指向C:\ProgramData），对当前用户时为current（$APPDATA指向C:\Users\xxx\AppData\Roaming）
  SetShellVarContext current
  # 由于更新时也会执行uninstall，所以判断如果AppData\Roaming中存在，则为update，不需要删除以下文件，否则为uninstall，需要删除以下文件（uninstall操作会先执行AppData\Roaming的删除，再执行customUnInstall）
  ${If} ${FileExists} `$APPDATA\yf-pc-chat-yfx\*.*`
    # 存在时则为更新，不删除
  ${Else}
    # 不存在时为卸载，需要删除
    Delete "$APPDATA\..\..\Documents\YFX\AppSetting.json"
    Delete "$APPDATA\..\..\Documents\YFX\UserInfo.json"
  ${EndIf}
  SetShellVarContext all
FunctionEnd

Function un.installDirReg
  ;卸载区段删除注册表中安装路径的键值
  DeleteRegKey /ifempty HKCU "Software\com.yf-pc-chat-yfx.install-guid"
FunctionEnd

Function .onInstSuccess
  ;把安装路径写入注册表，以便下次更新时，直接使用同样的安装路径，注意：需要在安装后的回调函数中写入（此函数不需要手动调用）
  WriteRegStr HKCU "Software\com.yf-pc-chat-yfx.install-guid" "" $INSTDIR
FunctionEnd

!macro customHeader
  ; 显示安装和卸载详细
  ShowInstDetails show
  ShowUninstDetails show
!macroend

!macro customInit
  Call checkYfscreenshot
  Call checkYfiletransfer
  Call checkMeeting
!macroend

!macro customUnInstall
  Call un.checkYfscreenshot
  Call un.checkYfiletransfer
  Call un.checkMeeting
  RMDir /r "$INSTDIR\resources"
  # 已在vue.config.js中通过deleteAppDataOnUninstall设置实现，由于更新时也会执行uninstall，所以不能在此执行
  # Call un.delAppData
  Call un.delDocument
  Call un.installDirReg
!macroend